﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;
using University.Workshop.Model;
using System.IO;

namespace University.Workshop.WinApp
{
    public partial class SearchUser : Form
    {
        Manager user = new Manager();
        bool userSelected;
        public SearchUser()
        {
            InitializeComponent();
            loadDataGridViewUser();
            userSelected = false;
            //IssuePermits();
        }
        //private void IssuePermits() 
        //{
        //    buttonModify.Enabled = RoleManager.PermissionUser(Main.userMenu[3], "ModifyUser");
        //    buttonDelete.Enabled = RoleManager.PermissionUser(Main.userMenu[3], "");
        //}

        private void loadDataGridViewUser()
        {
            dataGridViewSearchUser.DataSource = UserManager.LoadDataGridViewUser();
        }

        private void textBoxSearchUser_TextChanged(object sender, EventArgs e)
        {
            if (ValidateFields.SearchFieldValidation(textBoxSearchUser.Text))
            {
                dataGridViewSearchUser.DataSource = UserManager.LoadDataGridViewSearchUser(textBoxSearchUser.Text);
            }
            else
            {
                dataGridViewSearchUser.DataSource = null;
            }
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            if (userSelected)
            {
                ModifyUser modifyUser = new ModifyUser(user);
                modifyUser.ShowDialog();
                loadDataGridViewUser();
            }
            else
            {
                MessageBox.Show("Error");
            }            
        }

        private void dataGridViewSearchUser_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            LoadDataUser();
            userSelected = true;
        }

        private void LoadDataUser()
        {
            textBoxCI.Text = dataGridViewSearchUser.CurrentRow.Cells[0].Value.ToString();
            textBoxFirstName.Text = dataGridViewSearchUser.CurrentRow.Cells[1].Value.ToString();
            textBoxLastName.Text = dataGridViewSearchUser.CurrentRow.Cells[2].Value.ToString();
            textBoxAddress.Text = dataGridViewSearchUser.CurrentRow.Cells[3].Value.ToString();
            textBoxCellPhone.Text = dataGridViewSearchUser.CurrentRow.Cells[4].Value.ToString();
            textBoxPhone.Text = dataGridViewSearchUser.CurrentRow.Cells[5].Value.ToString();
            textBoxDate.Text = dataGridViewSearchUser.CurrentRow.Cells[6].Value.ToString();
            textBoxEmail.Text = dataGridViewSearchUser.CurrentRow.Cells[7].Value.ToString();
            textBoxGender.Text = dataGridViewSearchUser.CurrentRow.Cells[8].Value.ToString();
            textBoxUserName.Text = dataGridViewSearchUser.CurrentRow.Cells[9].Value.ToString();
            textBoxPassword.Text = dataGridViewSearchUser.CurrentRow.Cells[10].Value.ToString();
            try
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", dataGridViewSearchUser.CurrentRow.Cells[12].Value.ToString())));
                pictureBoxPicture.Image = picture;
            }
            catch (Exception)
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", "anonimos.jpg")));
                pictureBoxPicture.Image = picture;
            }
            
            user.CIManager = textBoxCI.Text;
            user.FirstName = textBoxFirstName.Text;            
            user.LastName = textBoxLastName.Text;
            user.Address = textBoxAddress.Text;
            user.CellPhone = int.Parse(textBoxCellPhone.Text);
            user.Phone = int.Parse(textBoxPhone.Text);
            user.DateInscription = Convert.ToDateTime(textBoxDate.Text);
            user.Email = textBoxEmail.Text;
            user.Gender = textBoxGender.Text;
            user.UserName = textBoxUserName.Text;
            user.Password = textBoxPassword.Text;
            user.ImageManager = dataGridViewSearchUser.CurrentRow.Cells[12].Value.ToString();

        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            DialogResult confirm = MessageBox.Show("Are you sure to disable this user?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
            if(confirm.Equals(DialogResult.OK))
            {
                if (!UserManager.DisableUser(textBoxCI.Text))
                {
                     MessageBox.Show("User enable");
                }
                loadDataGridViewUser();
            }
        }

        private void SearchUser_FormClosed(object sender, FormClosedEventArgs e)
        {
            Singleton.Instance.valid = true;
        }
    
    }
}
